package com.xywl.escort.view.home;

import com.xywl.escort.bean.BannerReq;
import com.xywl.escort.bean.BannerResp;
import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.IsNewMessageResp;
import com.xywl.escort.bean.IsRedPacketResp;
import com.xywl.escort.bean.MarketDataBean;
import com.xywl.escort.bean.MarketResp;
import com.xywl.escort.bean.MessageResp;
import com.xywl.escort.bean.NoticeResp;
import com.xywl.escort.bean.ProjectListResp;
import com.xywl.escort.bean.RedPacketReq;
import com.xywl.escort.bean.UpgradeReq;
import com.xywl.escort.bean.UpgradeResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import java.util.List;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class HomePresenter implements HomeContract.Presenter {
    private HomeContract.View mView;
    private ApiServer mApiServer;

    @Inject
    HomePresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(HomeContract.View view) {
        mView = view;
    }

    @Override
    public void loadBanner() {
        BannerReq req = new BannerReq();
        req.position = 1;
        mApiServer.focusPicture(req)
                .compose(RxUtil.<BannerResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BannerResp>bindToLife())
                .subscribe(new BaseSubscriber<BannerResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadBannerFailure();
                    }

                    @Override
                    public void onNext(BannerResp resp) {
                        mView.loadBannerSuccess(resp);
                    }
                });
    }

    @Override
    public void loadNotice(String userId) {
        BaseReq req = new BaseReq();
        req.userId = userId;
        mApiServer.noticeList(req)
                .compose(RxUtil.<NoticeResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<NoticeResp>bindToLife())
                .subscribe(new BaseSubscriber<NoticeResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadNoticeFailure("");
                    }

                    @Override
                    public void onNext(NoticeResp resp) {
                        if (null != resp && null != resp.data && resp.isOk()) {
                            mView.updateNotice(resp.data);
                        }
                    }
                });
    }

    @Override
    public void loadProjectList(int pageSize) {
        BaseReq req = new BaseReq();
        req.pageSize = pageSize;
        mApiServer.headerList(req)
                .compose(RxUtil.<ProjectListResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<ProjectListResp>bindToLife())
                .subscribe(new BaseSubscriber<ProjectListResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadProjectFailure();
                    }

                    @Override
                    public void onNext(ProjectListResp resp) {
                        if (null != resp && resp.isOk() && null != resp.data) {
                            ProjectListResp.DataBean dataBeans = resp.data;
                            mView.updateProjectList(dataBeans);
                        } else {
                            mView.loadProjectFailure();
                        }
                    }
                });
    }

    @Override
    public void loadMarketList(String userId) {
        BaseReq req = new BaseReq();
        req.userId = userId;
        mApiServer.bazaar(req)
                .compose(RxUtil.<MarketResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<MarketResp>bindToLife())
                .subscribe(new BaseSubscriber<MarketResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadMarketFailure();
                    }

                    @Override
                    public void onNext(MarketResp resp) {
                        if (null != resp && resp.isOk() && null != resp.data) {
                            List<MarketDataBean> dataBeans = resp.data;
                            mView.updateMarketList(dataBeans);
                        } else {
                            mView.loadMarketFailure();
                        }
                    }
                });
    }

    @Override
    public void findIsNewMessage(String userId) {
        BaseReq req = new BaseReq();
        req.userId = userId;
        mApiServer.findIsNewMessage(req)
                .compose(RxUtil.<IsNewMessageResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<IsNewMessageResp>bindToLife())
                .subscribe(new BaseSubscriber<IsNewMessageResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.findIsNewMessageFailure();
                    }

                    @Override
                    public void onNext(IsNewMessageResp resp) {
                        if (null != resp) {
                            if (null != resp.getData()) {
                                IsNewMessageResp.DataBean dataBeans = resp.getData();
                                if (null != dataBeans) {
                                    if (dataBeans.isNewMessage() == 1) {
                                        mView.newMessage();
                                    } else {
                                        mView.noNewMessage();
                                    }
                                } else {
                                    mView.findIsNewMessageFailure();
                                }
                            } else {
                                mView.findIsNewMessageFailure();
                            }
                        } else {
                            mView.findIsNewMessageFailure();
                        }
                    }
                });
    }

    @Override
    public void upgrade(int updateObject, final int versionCode) {
        UpgradeReq req = new UpgradeReq();
        req.updateObject = updateObject;
        mApiServer.upgrade(req)
                .compose(RxUtil.<UpgradeResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<UpgradeResp>bindToLife())
                .subscribe(new BaseSubscriber<UpgradeResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                    }

                    @Override
                    public void onNext(UpgradeResp resp) {
                        if (null != resp && null != resp.data) {
                            if (resp.isOk()) {
                                int inserVersionNumber = resp.data.innserVersionNumber;
                                if (inserVersionNumber > versionCode) {
                                    mView.hasUpgrade(resp.data);
                                }
                            }
                        }
                    }
                });
    }

    @Override
    public void isRedPacket(int bizCode) {
        RedPacketReq req = new RedPacketReq();
        req.bizCode = bizCode;
        mApiServer.isRedPacket(req)
                .compose(RxUtil.<IsRedPacketResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<IsRedPacketResp>bindToLife())
                .subscribe(new BaseSubscriber<IsRedPacketResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.isRedPacketFailure();
                    }

                    @Override
                    public void onNext(IsRedPacketResp resp) {
                        if (null != resp && resp.isOk() && null != resp.data) {
                            IsRedPacketResp.DataBean dataBean = resp.data;
                            if (dataBean.isPacket == 0) {
                                mView.isRedPacketSuccess(dataBean);
                            } else {
                                mView.isRedPacketFailure();
                            }
                        } else {
                            mView.isRedPacketFailure();
                        }
                    }
                });
    }

    @Override
    public void processRedPacket(int bizCode) {
        RedPacketReq req = new RedPacketReq();
        req.bizCode = bizCode;
        mApiServer.processRedPacket(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.processRedPacketFailure("请求失败，请稍后重试");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.processRedPacketSuccess(resp);
                            } else {
                                mView.processRedPacketFailure(resp.msg);
                            }
                        } else {
                            mView.processRedPacketFailure("请求失败，请稍后重试");
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("HomePresenter").e("unSubscribe");
        mView = null;
    }
}
